<%@ page language="java" contentType="text/html; charset=utf-8"  
import="com.brother.model.*,java.util.*" pageEncoding="utf-8"%>
<jsp:useBean id="service" class="com.brother.service.UserService"></jsp:useBean>
<jsp:useBean id="customer" class="com.brother.model.Customer"></jsp:useBean>

<jsp:setProperty property="*" name="customer"/>

<%

String action = request.getParameter("action");



boolean isEdit = false;
if(customer.getName() != null && action != null){
	String parentid = customer.getParentid();
	String superParentId = request.getParameter("superParentId");
	//  parent1, parent2,parent3,nearestParent
	if(superParentId != null && superParentId.length() > 0){
		parentid =  superParentId+"," + parentid;
	}
	if(action.equals("edit")){
		  service.updateUser(customer);
	}else{
		if(request.getParameter("password2").equals(customer.getPassword())){
			customer.setParentid(parentid);
			service.addUser(customer);
		}
	}
	response.sendRedirect("users.jsp");
	return;	
}
if(request.getParameter("password2") == null && "edit".equals(action)){
	customer = service.GetUserByUserId(Integer.parseInt(request.getParameter("userid")));
}

List<Customer> admins = service.getAllAdmins();


if(customer.getName() == null){
	customer = service.GetUserByUserId(customer.getUserid());
	isEdit = true;
}

%>

<%@page import="com.brother.service.UserService"%><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <script type='text/javascript' src='../dwr/engine.js'> </script>
  <script type='text/javascript' src='../dwr/util.js'> </script>
  <script type='text/javascript' src='../dwr/interface/UserService.js'> </script>
<title><%= isEdit ? "Edit":"Add" %> User</title>
<script type="text/javascript">
function check(form){
	if(form.password.value != form.password2.value){
		alert("The two password must be the same!");
		return false;
	}
	
	if(form.name.length < 1){
		alert("Name must not be empty!");
		return false;
	}
	
	return true;

}

function setSuperParentId(select){
	var value = select.options[select.selectedIndex].value;


	UserService.GetUserByUserId(value, function(model){
		if(model != null){
			document.getElementById("superParentId").value = model.parentid;
		}else{
			//isExistsName = false;
		}
	});
	
}
</script>
</head>
<body>
<form name="form1" method="post" action="addUser.jsp" onSubmit="return check(this);">
<input type="hidden" name="action" value="<%=action %>"/>
<input type="hidden" name="userid" value="<%=customer != null ? customer.getUserid(): -1 %>"/>
<table width="794" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="161">Login Name *</td>
    <td width="633"><input name="name"  type="text" value="<%=customer != null ? customer.getName():"" %>" id="name"></td>
  </tr>
  <tr>
    <td>Password *</td>
    <td><input name="password"  type="password" id="password"></td>
  </tr>
  <tr>
    <td>Confirm Password</td>
    <td><input name="password2"  type="password" id="password2"></td>
  </tr>
  <tr>
    <td>Email</td>
    <td><input name="email" type="text"  value="<%=customer != null ? customer.getEmail():"" %>" id="email"></td>
  </tr>
  <tr>
    <td>Superior</td>
    <td><select name="parentid"  onChange="setSuperParentId(this)" id="parentid">
    <option value=""></option>
    <%
    
    Customer superior = null;
    if(customer != null)
    	superior = service.getSuperiorUser(customer.getUserid()+"");
    for(int i = 0 ; i < admins.size(); i++){ 
    
    %>
    <option value="<%=admins.get(i).getUserid()%>" <%=admins.get(i).getUserid() == (superior != null ? superior.getUserid() : 0) ? "selected":"" %>><%=admins.get(i).getName()%></option>
    <%} %>
    </select>  <input type="hidden" value="" name="superParentId" id="superParentId"/>  </td>
  </tr>
  <tr>
    <td>Administrator</td>
    <td><select name="admin" id="isAdmin">
      <option value="false" <%=customer != null ? (customer.isAdmin()?"":"selected"):"" %>>No</option>
      <option value="true" <%=customer != null ? (customer.isAdmin()?"selected":""):"" %>>Yes</option>
    </select>
    </td>
  </tr>
  <tr>
    <td>Description</td>
    <td><textarea name="description" cols="80" rows="15" id="description"><%=customer != null ? customer.getDescription():"" %></textarea></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>
      <input type="submit" name="Submit" value="Ok">    </td>
  </tr>
</table>
</form>
</body>
</html>